﻿using App.Common;
using QxFramework.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadManager : MonoSingleton<LoadManager>
{
    // Start is called before the first frame update

    /*private void Start()
    {
        //Debug.Log(Data.Instance.TableAgent.GetString("Test",  "1", "Value"));
        LoadMapInfo(3, Vector3.zero);
    }*/



    public  LevelMap  LoadMapInfo(int LevelIndex,Vector3 MapPosition)
    {
        GameObject MapObject = Instantiate((GameObject)Resources.Load("Prefabs/NewPrefabs/Glevel"), Vector3.zero, Quaternion.identity);
        MapObject.transform.position = MapPosition;
        MapObject.transform.localPosition = Vector3.zero;
        TableAgent tableAgent = Data.Instance.TableAgent;
        List<hexagon> Cells = new List<hexagon>();
        for (int i=0;i< MapObject.transform.childCount; i++)
        {
            if (MapObject.transform.GetChild(i).GetComponent<hexagon>()&& MapObject.transform.childCount==19)
            {
                Cells.Add(MapObject.transform.GetChild(i).GetComponent<hexagon>());
            }
            else
            {
                Debug.Log("bug");
            } 
        }
        for(int i = 0; i < 19; i++)
        {
            if (IsEmptyLine(i, tableAgent, LevelIndex))
            {
                if(IsStartOrEnd(i, tableAgent, LevelIndex) != 0)
                {

                    if (IsStartOrEnd(i, tableAgent, LevelIndex) == 1)
                    {
                        hexagon BeforeCell = GetHexByName(i + 1, Cells);
                        Substitude(BeforeCell, "Start", MapObject).GetComponent<hexagon>().ChangeTheEdgeInfo("Start");


                    }
                    else if(IsStartOrEnd(i, tableAgent, LevelIndex) == -1)
                    {
                        hexagon BeforeCell = GetHexByName(i + 1, Cells);
                        Substitude(BeforeCell, "End", MapObject).GetComponent<hexagon>().ChangeTheEdgeInfo("End");
                    }
                    else
                    {
                        Debug.Log("bug");
                    }
                }
            }
            else
            {

                int In = int.Parse(tableAgent.GetString(LevelIndex.ToString(),  (i + 1).ToString(),"In"));
                int Out = int.Parse(tableAgent.GetString(LevelIndex.ToString(),(i + 1).ToString(), "Out" ));
                hexagon BeforeCell = GetHexByName(i + 1, Cells);
                GameObject temp=Substitude(BeforeCell,In.ToString()+"-"+Out.ToString(), MapObject);
                temp.GetComponent<hexagon>().edges[In] = Data.Edge.In;
                temp.GetComponent<hexagon>().edges[Out] = Data.Edge.Out;
            }
        }

        LevelMap map = MapObject.GetComponent<LevelMap>();
        map.InitLevel();

        return map;

        

    }
    private bool IsEmptyLine(int i,TableAgent tableAgent,int LevelIndex)
    {
        return tableAgent.GetString(LevelIndex.ToString(), (i + 1).ToString(), "In") == "" && tableAgent.GetString(LevelIndex.ToString(), (i + 1).ToString(), "Out") == "";
        
    }

    private int IsStartOrEnd(int i, TableAgent tableAgent, int LevelIndex)
    {
        if(tableAgent.GetString(LevelIndex.ToString(), (i + 1).ToString(),"StOrEnd") == "")
        {
            return 0;
        }
        else
        {
            return int.Parse(tableAgent.GetString(LevelIndex.ToString(), (i + 1).ToString(), "StOrEnd"));
        }
    }

    /*private Sprite SetImageByIndex(string s)
    {
        if (s == "Start")
        {
           return (Sprite) Resources.Load("Prefabs/NewPrefabs/MapImage/Start");
        }
        else if(s=="End")
        {
            return (Sprite)Resources.Load("Prefabs/NewPrefabs/MapImage/End");
        }
        else
        {
            int differ = (s[0] - s[2]+6) % 6;
            string Folder = (differ * 60).ToString()+"/";
            return (Sprite)Resources.Load("Prefabs/NewPrefabs/MapImage/" + Folder + s);
        }
    }*/

    private hexagon GetHexByName(int i, List<hexagon> Cells)
    {
        foreach(var j in Cells)
        {
            if (j.name == i.ToString())
            {
                return j;
            }
        }
        Debug.Log("bug");
        return null;
    }
    
    private GameObject Substitude(hexagon Before,string s,GameObject MapObject)
    {
        GameObject Current = null;
        if (s == "Start")
        {
            Current=Instantiate((GameObject)Resources.Load("Prefabs/NewPrefabs/Standard/Start"));
        }
        else if (s == "End")
        {
            Current = Instantiate((GameObject)Resources.Load("Prefabs/NewPrefabs/Standard/End"));
        }
        else {
            int differ = (s[0] - s[2] + 6) % 6;
            if (differ > 3)
            {
                differ = 6 - differ;
            }
            string Folder = (differ * 60).ToString();
            Current = Instantiate((GameObject)Resources.Load("Prefabs/NewPrefabs/Standard/"+Folder));
        }

        if(s != "Start"&& s != "End")
        {
            Vector3 v = Current.transform.localEulerAngles;
            int differ = (s[0] - s[2] + 6) % 6;
            if (differ > 3)
            {
                differ = 6 - differ;
            }
            if (differ == 3)
            {
                v.z += 60 * (2 - (s[0] - '0'));
            }
            else
            {
                int index = 1;
                if (((s[2] - '0' + differ) % 6 != (s[0]-'0')))//非基准，要翻
                {
                    index = -1;
                    v.y = 180;
                }
                v.z += 60 * index * ((index == -1 ? 3 : 2) - (s[0] - '0'));
            }
            
            Current.transform.localEulerAngles = v;
        }
        Current.transform.position = Before.transform.position;
        Current.GetComponent<hexagon>().Pos = Before.Pos;
        Current.name = Before.name;
        Current.transform.SetParent(MapObject.transform);
        Destroy(Before.gameObject);
        return Current;
    }
}
